package net.cyue.homework.student.management.servlet.student;


import jakarta.servlet.annotation.WebServlet;
import jakarta.validation.Valid;
import net.cyue.homework.student.management.dto.ClassAddDTO;
import net.cyue.homework.student.management.dto.DeleteDTO;
import net.cyue.homework.student.management.dto.ClassUpdateDTO;
import net.cyue.homework.student.management.entity.MajorClass;
import net.cyue.homework.student.management.http.HTTPServlet;
import net.cyue.homework.student.management.http.WebResult;
import net.cyue.homework.student.management.http.annotation.DeleteMethod;
import net.cyue.homework.student.management.http.annotation.PostMethod;
import net.cyue.homework.student.management.http.annotation.PutMethod;
import net.cyue.homework.student.management.util.DB;
import net.cyue.nest.nestjava.common.annotation.Body;
import net.cyue.nest.nestjava.common.annotation.Response;
import net.cyue.nest.nestjava.common.enums.HTTPStatus;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPRequest;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPResponse;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(
    name = "CLassServlet",
    value = {
        "/api/class",
        "/api/class/"
    }
)
public class ClassServlet extends HTTPServlet {
    @Override
    public void doGet(
        IHTTPRequest request,
        IHTTPResponse response
    ) throws IOException {
        String id = request.getParameter("id");
        if (id == null || id.isEmpty()) {
            response.code(HTTPStatus.BAD_REQUEST);
            response.send(WebResult.failed("id is required"));
            return;
        }

        try {
            ResultSet resultSet = DB.POOL.query("SELECT * FROM major_class WHERE id = " + id + " LIMIT 1");
            if (!resultSet.next()) {
                resultSet.close();
                response.code(HTTPStatus.NOT_FOUND);
                response.send(WebResult.failed("班级不存在"));
            }
            int classID = resultSet.getInt("id");
            int majorID = resultSet.getInt("major_id");
            String name = resultSet.getString("name");
            MajorClass majorClass = new MajorClass(classID, majorID, name);
            response.send(WebResult.success("获取成功", majorClass));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，获取失败"));
        }
    }

    @PostMethod
    public void addClass(
        @Response IHTTPResponse response,
        @Valid @Body ClassAddDTO dto
    ) throws IOException {

        try {
            int rows = DB.POOL.transactionPreparedUpdate(
            "INSERT INTO major_class (major_id, name) VALUES (?, ?)",
                new Object[]{
                    dto.getMajorId(),
                    dto.getName()
                }
            );
            if (rows == 0) {
                response.send(WebResult.failed("添加失败"));;
                return;
            }
            response.send(WebResult.success("添加成功"));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，添加失败"));
        }

    }


    @PutMethod
    public void updateClass(
        @Response IHTTPResponse response,
        @Valid @Body ClassUpdateDTO dto
    ) throws IOException {
        try {
            int rows = DB.POOL.transactionPreparedUpdate(
            "UPDATE major_class SET major_id = ?, name = ? WHERE id = ?",
                new Object[]{
                    dto.getMajorId(),
                    dto.getName(),
                    dto.getId()
                }
            );
            if (rows == 0) {
                response.send(WebResult.failed("更新失败"));
                return;
            }
            response.send(WebResult.success("更新成功"));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，更新失败"));
        }
    }

    @DeleteMethod
    public void deleteClass(
        @Response IHTTPResponse response,
        @Valid @Body DeleteDTO dto
    ) throws IOException {
        try {
            int rows = DB.POOL.transactionUpdate("DELETE FROM major_class WHERE id = " + dto.getId());
            if (rows == 0) {
                response.send(WebResult.failed("删除失败"));
                return;
            }
            response.send(WebResult.success("删除成功"));
        } catch (SQLException e) {
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，删除失败"));
        }
    }

}

